package com.king.zxing.ui;


import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.king.zxing.lite.CaptureActivity;
import com.king.zxing.R;
import com.king.zxing.lite.Intents;
import com.king.zxing.util.BitmapUtil;
import com.king.zxing.util.CodeUtils;
import com.king.zxing.util.DensityUtil;
import com.king.zxing.util.LayoutUtils;
import com.king.zxing.util.RealPathUtil;
import com.king.zxing.util.SystemUtil;

public class BJGCaptureActivity extends CaptureActivity {
    private static final String TAG = "BJGCaptureActivity";
    private LinearLayout actionBar;
    private ImageView pictureSelect;
    private ImageView flashSelect;
    private boolean openFlash = false;

    @Override
    public void initUI() {
        super.initUI();
        pictureSelect = findViewById(R.id.zxing_picture_select_icon);
        flashSelect = findViewById(R.id.zxing_flash_icon);
        actionBar = findViewById(R.id.action_bar);
        initViewPosition();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bjgcapture;
    }

    //初始化ImageView位置
    private void initViewPosition() {
        int screenWidth = LayoutUtils.screenWidth(this);
        int halfLeftMargin = (screenWidth - DensityUtil.dip2px(this, 55 + 55 + 70)) / 2;
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) pictureSelect.getLayoutParams();
        layoutParams.leftMargin = halfLeftMargin;
        if (!SystemUtil.hasFlash(this)) {
            flashSelect.setImageBitmap(BitmapUtil.getBitMap(this, R.mipmap.zxing_flash_unavailable_icon));
            flashSelect.setClickable(false);
        }
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBar.getLayoutParams();
        params.topMargin = LayoutUtils.statusBarHeight(getApplicationContext());
    }

    public void flashClick(View v) {
        if (openFlash) {
            flashSelect.setImageBitmap(BitmapUtil.getBitMap(this, R.mipmap.zxing_flash_normal_icon));
            getCameraManager().closeFlash();
            openFlash = false;
        } else {
            flashSelect.setImageBitmap(BitmapUtil.getBitMap(this, R.mipmap.zxing_flash_selected_icon));
            getCameraManager().openFlash();
            openFlash = true;
        }
    }

    public void pictureClick(View v) {

        getPhoto();
    }

    public void back(View v) {
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BitmapUtil.clear();
    }

    private void getPhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, 0x1026);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x1026 && resultCode == RESULT_OK){
            Log.d(TAG, "onActivityResult`: data.getData(): "+data.getData());
            Log.d(TAG, "onActivityResult: data.getData() real path: " + RealPathUtil.getRealPathFromUri(this,data.getData()));
//            Intent intent = new Intent();
//            intent.putExtra("bitmap_path",RealPathUtil.getRealPathFromUri(this,data.getData()));
//            setResult(RESULT_OK,intent);
//            finish();

            String qrs = CodeUtils.parseCode(RealPathUtil.getRealPathFromUri(this,data.getData()));
            if(qrs == null){
                setTips(getString(R.string.bjg_capture_search_failed));
            }else{
                Intent intent = new Intent();
                intent.putExtra(Intents.Scan.RESULT, qrs);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        }
    }
}
